import pygame
import random,sys,math

#virtical shotting game...

STARNUM = 80
MAXOBJ = 90
GUNFIRETOP = 130
TOTALFIRETOP = 140

class obj:

    GUNFROFFSETV = 10
    GUNFROFFSETH = 14
    NON_TYPE   = 0
    GUN_FIRE   = 1
    ENEMY      = 2
    STAR       = 3
    FLAME1     = 4      
    FLAME2     = 5      
    FLAME3     = 6
    ENEMYFR    = 7
    TRACELEN   = 20

    def __init__(self, length, height, objlist):
        self.type = 0
        self.belongto = None
        self.tracelen = 0
        self.objlist = objlist
        self.smart = 0
        self.v    = 0 #virtical speed
        self.h    = 0 #h....... spead
        self.length = length
        self.height = height
        self.image  = None
        self.imagerect  = None
        self.target = None

    def set_type(self, t, target = None, belongto = None):
        
        self.type = t

        if (self.type == self.GUN_FIRE and target != None):
            self.image = pygame.image.load("gun_fire.png")
            self.belongto = belongto
            self.imagerect = self.image.get_rect().move(target.left + self.GUNFROFFSETV,target.top + self.GUNFROFFSETH)
            self.v = 25
        elif (self.type == self.ENEMY):
            self.image = pygame.image.load("enemy.png")
            self.target = target
            self.tracelen = self.TRACELEN
            self.imagerect = self.image.get_rect().move(random.randint(20,self.length - 20),-random.randint(10,400))
            self.v = 7
        elif (self.type == self.STAR):
            self.image = pygame.image.load("star.png")
            self.imagerect = self.image.get_rect().move(random.randint(0,self.length),random.randint(0,self.height))
            self.v = random.randint(3,6)
        elif (self.type == self.FLAME1):
            self.image = pygame.image.load("flame1.png")
            self.imagerect = self.image.get_rect().move(target.left - self.imagerect.width / 2, target.top - self.imagerect.height / 2)
            self.tracelen = 3#last time counter
        elif (self.type == self.FLAME2):
            self.image = pygame.image.load("flame2.png")
            self.imagerect = self.image.get_rect().move(target.left, target.top)
            self.tracelen = 3#last time counter
        elif (self.type == self.FLAME3):
            self.image = pygame.image.load("flame3.png")
            self.imagerect = self.image.get_rect().move(target.left, target.top)
            self.tracelen = 3#last time counter
        elif (self.type == self.ENEMYFR):
            self.image = pygame.image.load("enemy_fire.png")

            if (belongto != None and target.alive):
                self.imagerect = self.image.get_rect().move(belongto.imagerect.center) # the belong here is the location of the enemy craft
                tcen = list(target.imagerect.center)
                bcen = list(belongto.imagerect.center)

                dv = abs(tcen[1] - bcen[1])
                dh = abs(tcen[0] - bcen[0])

                if (dv != 0):
                    r = float(dh) / float(dv)
                    self.v = 10.0 / float(math.sqrt(r ** 2 + 1))
                    self.h = r * self.v
                else:
                    self.v = 10
                    self.h = 0
            else:
                self.v = 10
                self.h = 0

            if (tcen[0] < bcen[0]):
                self.h = -self.h
            
            if (tcen[1] < bcen[1]):
                self.v = -self.v

        else:
            pass

    def fireoutthehole(self, target, belongto = None):
        for i in range(GUNFIRETOP, TOTALFIRETOP):
            if (self.objlist[i].type == self.objlist[i].NON_TYPE):
                self.objlist[i].set_type(self.objlist[i].ENEMYFR, target, belongto)
                return
    def update(self, playerlist = None):
        if (self.type == self.GUN_FIRE):                            # GUN FIRE PROCCESS
            if (self.imagerect.bottom - self.v <= 0):
                self.type = self.NON_TYPE
                return

            self.imagerect = self.imagerect.move(0,-self.v)

            kill = False    #if kill somebody
            mostfront = 0   #most front death
            killindex = -1  #where is the unlucky guy

            for i in range(STARNUM, MAXOBJ + 1):
                if (self.objlist[i].type == self.ENEMY
                    and (                                                                       # if there is any
                        (self.objlist[i].imagerect.bottom >= self.imagerect.top                 # intersection
                        and self.objlist[i].imagerect.bottom <= self.imagerect.bottom)           
                    or
                        (self.objlist[i].imagerect.top >= self.imagerect.top
                        and self.objlist[i].imagerect.top <= self.imagerect.bottom)
                    or
                        (self.objlist[i].imagerect.top >= self.imagerect.top
                         and self.objlist[i].imagerect.bottom <= self.imagerect.bottom)
                    or
                        (self.objlist[i].imagerect.top <= self.imagerect.top
                         and self.objlist[i].imagerect.bottom >= self.imagerect.bottom)
                        )
                    and (
                        (self.imagerect.left >= self.objlist[i].imagerect.left
                        and self.imagerect.left <= self.objlist[i].imagerect.right)
                    or
                        (self.imagerect.right >= self.objlist[i].imagerect.left
                        and self.imagerect.right <= self.objlist[i].imagerect.right)
                    or
                        (self.imagerect.left >= self.objlist[i].imagerect.left
                         and self.imagerect.right <= self.objlist[i].imagerect.right)
                    or
                        (self.imagerect.left <= self.objlist[i].imagerect.left
                         and self.imagerect.right >= self.objlist[i].imagerect.right)
                        )
                    ):#Kill the enemy
                        if (self.objlist[i].imagerect.bottom > mostfront):
                            mostfront = self.objlist[i].imagerect.bottom
                            killindex = i
                            kill = True
            if (kill):
                if (self.belongto != None):
                    self.belongto.pcnt.score += 10
                self.belongto = None
                self.type = self.NON_TYPE
                self.objlist[killindex].set_type(self.FLAME1,self.objlist[killindex].imagerect)
                self.objlist[killindex].smart = 0 #make it stupid! 
                    
        elif (self.type == self.ENEMY):                            # ENEMY PROCCESS

            if (self.imagerect.top + self.v > self.height):
                self.type = self.NON_TYPE
                self.smart = 0 #make it stupid!
            else:
                if (self.smart == 1 and self.imagerect.bottom >= 0 and self.target != None and self.target.alive):
                    if (self.tracelen and self.imagerect.bottom <= self.target.imagerect.top):
                        if (list(self.imagerect.center)[0] < list(self.target.imagerect.center)[0] - self.v):
                            self.tracelen -= 1
                            self.imagerect = self.imagerect.move(self.v,0)
                        elif (list(self.imagerect.center)[0] > list(self.target.imagerect.center)[0] + self.v):
                            self.tracelen -= 1
                            self.imagerect = self.imagerect.move(-self.v,0)
                self.imagerect = self.imagerect.move(0,self.v)

                if (self.smart == 2 and self.imagerect.bottom >= 0 and self.target != None and self.target.alive):
                    if (random.randint(1,20) == 1):
                        self.fireoutthehole(self.target,self) #fire to the player
                    if (self.tracelen and self.imagerect.bottom <= self.target.imagerect.top):
                        if (list(self.imagerect.center)[0] < list(self.target.imagerect.center)[0] - self.v):
                            self.tracelen -= 1
                            self.imagerect = self.imagerect.move(self.v,0)
                        elif (list(self.imagerect.center)[0] > list(self.target.imagerect.center)[0] + self.v):
                            self.tracelen -= 1
                            self.imagerect = self.imagerect.move(-self.v,0)
                self.imagerect = self.imagerect.move(0,self.v)

            for item in playerlist:
                if (item.alive):
                    if (self.imagerect.colliderect(item.imagerect)):# Crash with player
                            self.set_type(self.FLAME1,self.imagerect)
                            self.smart = 0 #make it stupid! 
                            item.alive = False# The player is dead
                            return

        elif (self.type == self.STAR):                            # STAR PROCCESS
            if (self.imagerect.top + self.v > self.height):
                self.imagerect = self.image.get_rect().move(random.randint(0,self.length),-random.randint(10,150))
            else:
                self.imagerect = self.imagerect.move(0,self.v)

        elif (self.type == self.FLAME1):                            # FLAME PROCCESS
            if (self.tracelen):
                self.tracelen -= 1
                if (self.imagerect.top + self.v > self.height):
                    self.type = self.NON_TYPE
                    self.smart = 0 #make it stupid!
                else:
                    self.imagerect = self.imagerect.move(0,self.v)
            else:
                self.set_type(self.FLAME2,self.imagerect)
        elif (self.type == self.FLAME2):                            # FLAME PROCCESS
            if (self.tracelen):
                self.tracelen -= 1
                if (self.imagerect.top + self.v > self.height):
                    self.type = self.NON_TYPE
                    self.smart = 0 #make it stupid!
                else:
                    self.imagerect = self.imagerect.move(0,self.v)
            else:
                self.set_type(self.FLAME3,self.imagerect)
        elif (self.type == self.FLAME3):                            # FLAME PROCCESS
            if (self.tracelen):
                self.tracelen -= 1
                if (self.imagerect.top + self.v > self.height):
                    self.type = self.NON_TYPE
                    self.smart = 0 #make it stupid!
                else:
                    self.imagerect = self.imagerect.move(0,self.v)
            else:
                self.set_type(self.NON_TYPE)
                self.tracelen = 0
                self.smart = 0
        elif (self.type == self.ENEMYFR):                            # ENEMY FIRE PROCCESS

            if ((self.imagerect.top + self.v >= self.height and self.imagerect.bottom + self.v > self.height)
                or
                (self.imagerect.top + self.v < 0 and self.imagerect.bottom + self.v <= 0)
                or
                (self.imagerect.left + self.h < 0 and self.imagerect.right + self.h <= 0)
                or
                (self.imagerect.right + self.h > self.length and self.imagerect.left + self.h >= self.length)):
                self.type = self.NON_TYPE
                return

            self.imagerect = self.imagerect.move(self.h, self.v)

            for item in playerlist:
                if (item.alive):
                    if (self.imagerect.colliderect(item.imagerect)):# Crash with player
                            self.set_type(self.FLAME1,self.imagerect)
                            self.smart = 0 #make it stupid! 
                            item.alive = False# The player is dead
                            return
        else:
            pass

    def show(self, screen):
        if (self.type == self.STAR
            or self.type == self.ENEMY
            or self.type == self.GUN_FIRE
            or self.type == self.FLAME1
            or self.type == self.FLAME2
            or self.type == self.FLAME3
            or self.type == self.ENEMYFR):
            screen.blit(self.image, self.imagerect)
